package com.book.lizhi.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.waps.AppConnect;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.book.lizhi.AppApplication;
import com.book.lizhi.BaseActivity;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.database.DataBaseMannager;
import com.book.lizhi.http.HttpUtil;
import com.book.lizhi.model.ArticleModel;
import com.book.lizhi.util.AppLog;
import com.book.lizhi.widget.HttpAsyncLayout;
import com.book.lizhi.widget.HttpAsyncLayout.OnHttpAsyncListner;

public class ArticleActivity extends BaseActivity {

	private HttpAsyncLayout mAsyncLayout;
	private TextView mContentView;
	private TextView mSourceView;
	private ArticleModel mArticleModel;

	private View mShareView;
	private View mFavView;
	private TextView mShowFavView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_article);

		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);
		mArticleModel = (ArticleModel) getIntent().getSerializableExtra(Constants.ARTICLE);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setTitle(mArticleModel.name);

		mAsyncLayout = (HttpAsyncLayout) findViewById(R.id.http_async_layout);

		mContentView = (TextView) findViewById(R.id.article_content);
		Typeface typeface = Typeface.createFromAsset(getAssets(), "xin_hei.otf");
		mContentView.setTypeface(typeface);
		mContentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);

		mSourceView = (TextView) findViewById(R.id.artice_source);

		mFavView = findViewById(R.id.favorite_container);
		mShowFavView = (TextView) findViewById(R.id.article_fav_view);
		if (mArticleModel.isFav) {
			mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_press_favorite, 0, 0, 0);
		} else {
			mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_favorite, 0, 0, 0);
		}
		mShareView = findViewById(R.id.share_container);

		addOnListener();

		// 互动广告调用方式
		LinearLayout layout = (LinearLayout) this.findViewById(R.id.AdLinearLayout);
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AppLog.e("into--adClick");
				Toast.makeText(ArticleActivity.this, "YES", Toast.LENGTH_SHORT).show();
			}
		});
		AppConnect.getInstance(this).showBannerAd(this, layout);

		sendGetRequest(HttpUtil.getArticleContentURL(mArticleModel.type, mArticleModel.id), mAsyncLayout);

	}

	private void addOnListener() {
		mSourceView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
				intent.putExtra(Constants.SOURCE_URL, mArticleModel.href);
				startActivity(intent);
			}
		});
		mAsyncLayout.setOnHttpAsyncListner(new OnHttpAsyncListner() {

			@Override
			public void onSuccessListener(String url, String response) {
				AppApplication.instance.jsonCache.saveJsonStr(url, response);
				mContentView.setText(JSON.parse(response).toString());
			}

			@Override
			public void onRefreshListener() {
				sendGetRequest(HttpUtil.getArticleContentURL(mArticleModel.type, mArticleModel.id), mAsyncLayout);
			}

			@Override
			public void onErrorListener(VolleyError error) {
				AppLog.e(error.toString());
			}
		});
		mFavView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mArticleModel.isFav) {
					new AlertDialog.Builder(ArticleActivity.this).setTitle("取消收藏").setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_favorite, 0, 0, 0);
							mArticleModel.isFav = false;
							DataBaseMannager.getInstance().deleteFavById(ArticleActivity.this, mArticleModel.id);
							Intent intent = new Intent(Constants.DELETE_FAV);
							intent.putExtra("id", mArticleModel.id);
							LocalBroadcastManager.getInstance(ArticleActivity.this).sendBroadcast(intent);
						}
					}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).show();
				} else {
					mShowFavView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_press_favorite, 0, 0, 0);
					mArticleModel.isFav = true;
					DataBaseMannager.getInstance().insertFave(ArticleActivity.this, mArticleModel);
					Intent intent = new Intent(Constants.FAV);
					intent.putExtra("id", mArticleModel.id);
					LocalBroadcastManager.getInstance(ArticleActivity.this).sendBroadcast(intent);

					Toast.makeText(ArticleActivity.this, "收藏成功!", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mShareView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT, "我在励志应用中看到一篇很有用的文章《" + mArticleModel.name + "》下载励志应用看完整文章。http://www.wandoujia.com/apps/com.book.lizhi");
				sendIntent.setType("text/plain");
				startActivity(Intent.createChooser(sendIntent, "励志文章分享"));
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
